package com.ffcc.fitness.websocket.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.ffcc.fitness.websocket.pojo.Chat;
import com.ffcc.fitness.websocket.pojo.Message;
import com.ffcc.fitness.websocket.service.ChatService;
import com.ffcc.fitness.base.response.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天控制器
 */
@RestController
@RequestMapping("/v1/chat")
@SaCheckLogin
public class ChatController {
    
    @Autowired
    private ChatService chatService;
    
    /**
     * 获取聊天记录
     */
    @GetMapping("/history")    public JsonResult getChatHistory(@RequestParam Long userId, 
                                          @RequestParam Long otherUserId,
                                          @RequestParam(defaultValue = "50") Integer limit) {
        List<Message> messages = chatService.getChatHistory(userId, otherUserId, limit);
        return JsonResult.ok(messages);
    }
    
    /**
     * 标记消息为已读
     */
    @PostMapping("/read")    public JsonResult markAsRead(@RequestParam Long fromId, @RequestParam Long toId) {
        chatService.markAsRead(fromId, toId);
        return JsonResult.ok();
    }
    
    /**
     * 获取未读消息数
     */
    @GetMapping("/unread")    public JsonResult getUnreadCount(@RequestParam Long userId) {
        int count = chatService.getUnreadCount(userId);
        return JsonResult.ok(count);
    }
      /**
     * 获取用户的会话列表
     */
    @GetMapping("/sessions")
    public JsonResult getUserSessions() {
        // 从SaToken获取当前登录用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        List<Chat> sessions = chatService.getUserSessions(userId);
        return JsonResult.ok(sessions);
    }    /**
     * 创建或获取聊天会话
     */
    @PostMapping("/create")
    public JsonResult createChat(@RequestBody CreateChatRequest request) {
        System.out.println("=== 接收到创建聊天会话请求 ===");
        System.out.println("请求参数: " + request);
        System.out.println("sellerId: " + request.getSellerId());
        System.out.println("sellerName: " + request.getSellerName());
        System.out.println("equipmentId: " + request.getEquipmentId());
        System.out.println("equipmentTitle: " + request.getEquipmentTitle());
        
        // 从SaToken获取当前登录用户ID
        Long buyerId = StpUtil.getLoginIdAsLong();
        System.out.println("当前买家ID: " + buyerId);
        
        Chat chat = chatService.createOrGetChat(buyerId, request.getSellerId(), 
                                              request.getEquipmentId(), request.getEquipmentTitle());
        System.out.println("创建的会话: " + chat);
        return JsonResult.ok(chat);
    }/**
     * 获取未读消息总数
     */
    @GetMapping("/unread-count")
    public JsonResult getUnreadMessageCount() {
        // 从SaToken获取当前登录用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        
        int count = chatService.getUnreadCount(userId);
        return JsonResult.ok(count);
    }    /**
     * WebSocket连接测试端点
     */
    @GetMapping("/test-websocket")
    public JsonResult testWebSocket() {
        Map<String, Object> info = new HashMap<>();
        info.put("websocketEndpoint", "ws://localhost:8080/websocket/chat/{userId}");
        info.put("currentUserId", StpUtil.getLoginIdAsLong());
        info.put("tokenValue", StpUtil.getTokenValue());
        info.put("status", "Spring WebSocket聊天服务正常运行");
        return JsonResult.ok(info);
    }
}

/**
 * 创建聊天请求对象
 */
class CreateChatRequest {
    private Long sellerId;
    private String sellerName;
    private Long equipmentId;
    private String equipmentTitle;
    
    // getter和setter方法
    public Long getSellerId() { return sellerId; }
    public void setSellerId(Long sellerId) { this.sellerId = sellerId; }
    
    public String getSellerName() { return sellerName; }
    public void setSellerName(String sellerName) { this.sellerName = sellerName; }
    
    public Long getEquipmentId() { return equipmentId; }
    public void setEquipmentId(Long equipmentId) { this.equipmentId = equipmentId; }
      public String getEquipmentTitle() { return equipmentTitle; }
    public void setEquipmentTitle(String equipmentTitle) { this.equipmentTitle = equipmentTitle; }
}
